#pragma once
#include "../../test_type.h"
#include "../../../src/socket/TcpIoSocket.h"
#include "../../../src/stream/SocketIoBuffer.h"
#include "../../../src/socket/TcpIoClient.h"
#include "../../../src/algorithm/SList.h"
#include <iostream>
#include <memory>
#include <mutex>

namespace client
{
	using namespace std;
	using namespace LibTools;
	class MessageClient
	{
	public:
		MessageClient();
		~MessageClient();

		void Connect(const string& addr, int port);

		void Write(const char* str, size_t size);

		void SetTcpServer(CBasicService* server);

		void Close();

		bool Run();

		string GetMessage();
	protected:
		static DWORD HandleConnect(CSockError ec, void* invoker);

		static DWORD HandleWrite(char* data, DWORD dwSize, CSockError ec, void* invoker);

		static DWORD HandleRead(char* data, DWORD dwSize, CSockError ec, void* invoker);

		static DWORD HandlePost(void *invoker);

		static DWORD HandleClose(void* invoker);
	public:
		CSocketIoBuffer _readbuff;
		CSocketIoBuffer _writebuff;

		CSocketIoBuffer _writePost;
		CSocketIoBuffer _closePost;
		shared_ptr<CTcpIoClient> _tcpServer;
		CTcpIoSocket _sock;

		mutex _mutex;
		SList<string> _msgs;
	};
}